package com.ls.blog.controller;

import com.ls.blog.annotation.UserLoginToken;
import com.ls.blog.annotation.LogAnnotation;
import com.ls.blog.vo.ArchiveVo;
import com.ls.blog.result.JsonResult;
import com.ls.blog.api.ArticleService;
import com.ls.blog.utils.ResultTool;
import com.ls.blog.vo.ArticleVo;
import com.ls.blog.vo.params.ArticleParam;
import com.ls.blog.vo.params.PageParams;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: myblog
 * @author: lishun
 * @create: 2021-07-04 23:59
 **/
@Api(tags = {"文章模块"})
@Slf4j
@RestController
@RequestMapping("articles")
public class ArticleController {

    @Autowired
    private ArticleService
            articleService;

    /**
     * 接收分页查询参数,返回数据
      * @param pageParams
     * @return
     */
    @ApiOperation("文章列表")
    @ApiImplicitParam(name = "PageParams",value = "1",dataType = "PageParams")
    @LogAnnotation(module = "文章模块",operation = "获得文章列表")
    @PostMapping
    public JsonResult<List<ArticleVo>> listArticles(@RequestBody PageParams pageParams) {

        return ResultTool.success(articleService.listArticlesPage(pageParams)) ;
    }



    /**
     * 热门文章
     * @return
     */
    @ApiOperation("最热文章")
    @LogAnnotation(module = "文章模块",operation = "获得最热文章")
    @PostMapping("hot")
    public JsonResult<List<ArticleVo>> hotArticles() {
        int limit = 5;
        return ResultTool.success(articleService.hotArticles(limit));
    }

    /**
     * 最新文章
     * @return
     */
    @ApiOperation("最新文章")
    @LogAnnotation(module = "文章模块",operation = "获得最新文章")
    @PostMapping("new")
    public JsonResult<List<ArticleVo>> newArticles() {
        int limit = 5;
        return ResultTool.success(articleService.newArticles(limit));
    }

    /**
     * 文章归档
     * @return
     */
    @ApiOperation("文章归档")
    @LogAnnotation(module = "文章模块",operation = "获得文章归档")
    @GetMapping("listArchives")
    public JsonResult<ArchiveVo> listArchives(Integer current) throws Exception {
        ArchiveVo archives = articleService.listArchives(current);

        return ResultTool.success(archives);
    }

    /**
     * 根据id返回数据
     * @param id
     * @return
     */
    @ApiOperation("文章详情")
    @LogAnnotation(module = "文章模块",operation = "根据id获得文章详细信息")
    @GetMapping("view/{id}")
    public JsonResult<ArticleVo> findArticleById(@PathVariable("id") Long id){
         ArticleVo articleVo =  articleService.findArticleById(id);
         return ResultTool.success(articleVo);
    }


    @ApiOperation("文章发表")
    @LogAnnotation(module = "文章模块",operation = "发表文章")
    @PostMapping("/publish")
    @UserLoginToken
    public JsonResult<Map<String,String>> publish(@RequestBody ArticleParam articleParam){
        Long id = articleService.publish(articleParam);
        Map<String, String> map = new HashMap<>();
        map.put("id",id.toString());
        return ResultTool.success(map);
    }






}